﻿using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp.WinForms;
using NymphsLib.CommonUtilities;
using NymphsLib.WebBrowserController;

namespace ImdbBrowser
{
    public partial class ImdbBrowserControl : UserControl
    {
        public ImdbBrowserControl()
        {
            InitializeComponent();
            BrowserWindowManager = new WindowManager2(tabControl);
            BrowserWindowManager.CommandStateChanged += WindowManagerCommandStateChanged;
            BrowserWindowManager.StatusTextChanged += WindowManagerStatusTextChanged;
            BrowserWindowManager.DocumentCompleted += BrowserWindowManagerDocumentCompleted;
        }

        void BrowserWindowManagerDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            InvokeDocumentCompleted(sender, e);
        }

        // Update the status text
        void WindowManagerStatusTextChanged(object sender, TextChangedEventArgs e)
        {
            toolStripStatusLabel.Text = e.Text;
        }

        // Enable / disable buttons
        void WindowManagerCommandStateChanged(object sender, CommandStateEventArgs e)
        {
            forwardToolStripButton.Enabled = ((e.BrowserCommands & BrowserCommands.Forward) == BrowserCommands.Forward);
            backToolStripButton.Enabled = ((e.BrowserCommands & BrowserCommands.Back) == BrowserCommands.Back);
            //            printPreviewToolStripButton.Enabled = ((e.BrowserCommands & BrowserCommands.PrintPreview) == BrowserCommands.PrintPreview);
            //this.printPreviewToolStripMenuItem.Enabled = ((e.BrowserCommands & BrowserCommands.PrintPreview) == BrowserCommands.PrintPreview);
            //            printToolStripButton.Enabled = ((e.BrowserCommands & BrowserCommands.Print) == BrowserCommands.Print);
            //this.printToolStripMenuItem.Enabled = ((e.BrowserCommands & BrowserCommands.Print) == BrowserCommands.Print);
            homeToolStripButton.Enabled = ((e.BrowserCommands & BrowserCommands.Home) == BrowserCommands.Home);
            //            searchToolStripButton.Enabled = ((e.BrowserCommands & BrowserCommands.Search) == BrowserCommands.Search);
            refreshToolStripButton.Enabled = ((e.BrowserCommands & BrowserCommands.Reload) == BrowserCommands.Reload);
            stopToolStripButton.Enabled = ((e.BrowserCommands & BrowserCommands.Stop) == BrowserCommands.Stop);
        }

        private void CloseWindowToolStripButtonClick(object sender, EventArgs e)
        {
            BrowserWindowManager.New();
        }

        private void BackToolStripButtonClick(object sender, EventArgs e)
        {
            if (BrowserWindowManager.ActiveBrowser != null && BrowserWindowManager.ActiveBrowser.CanGoBack)
                BrowserWindowManager.ActiveBrowser.Back();
        }

        private void ForwardToolStripButtonClick(object sender, EventArgs e)
        {
            if (BrowserWindowManager.ActiveBrowser != null && BrowserWindowManager.ActiveBrowser.CanGoForward)
                BrowserWindowManager.ActiveBrowser.Forward();
        }

        private void StopToolStripButtonClick(object sender, EventArgs e)
        {
            if (BrowserWindowManager.ActiveBrowser != null)
            {
                BrowserWindowManager.ActiveBrowser.Stop();
            }
            stopToolStripButton.Enabled = false;
        }

        private void RefreshToolStripButtonClick(object sender, EventArgs e)
        {
            if (BrowserWindowManager.ActiveBrowser != null)
            {
                BrowserWindowManager.ActiveBrowser.Refresh();
            }
        }

        private void HomeToolStripButtonClick(object sender, EventArgs e)
        {
            if (BrowserWindowManager.ActiveBrowser != null)
                BrowserWindowManager.ActiveBrowser.Load("about:blank");
        }

        private void SearchToolStripButtonClick(object sender, EventArgs e)
        {
            //            if (BrowserWindowManager.ActiveBrowser != null)
            //                BrowserWindowManager.ActiveBrowser.GoSearch();
        }

        private void PrintToolStripButtonClick(object sender, EventArgs e)
        {
            Print();
        }
        private void PrintPreviewToolStripButtonClick(object sender, EventArgs e)
        {
            PrintPreview();
        }
        private void CloseToolStripButtonClick(object sender, EventArgs e)
        {
            BrowserWindowManager.Close();
        }
        #region Printing & Print Preview
        private void Print()
        {
            var brw = BrowserWindowManager.ActiveBrowser;
            if (brw != null)
                brw.Print();
        }

        private void PrintPreview()
        {
            var brw = BrowserWindowManager.ActiveBrowser;
//            if (brw != null)
//                brw.Print();
        }
        #endregion

        private void TabControlVisibleChanged(object sender, EventArgs e)
        {
            panel1.BackColor = tabControl.Visible ? SystemColors.Control : SystemColors.AppWorkspace;
        }

        private void TabbedBrowserControlLoad(object sender, EventArgs e)
        {

        }

        public WindowManager2 BrowserWindowManager { get; private set; }

        public string DocumentText
        {
            get
            {
                string html = TaskHelper.RunTaskSynchronously(BrowserWindowManager.ActiveBrowser.GetSourceAsync());
                return html;
            }
        }

        public event WebBrowserDocumentCompletedEventHandler DocumentCompleted;
        public void InvokeDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var handler = DocumentCompleted;
            if (handler != null) handler(sender, e);
        }

        public void NavigateNew(string url)
        {
            BrowserWindowManager.New(url);
        }

        public void Navigate(string url)
        {
            ChromiumWebBrowser active = BrowserWindowManager.ActiveBrowser ?? BrowserWindowManager.New();
            active.Load(url);
        }

        private void IntelligenceToolStripButtonClick(object sender, EventArgs e)
        {
            intelligenceToolStripButton.Checked = !intelligenceToolStripButton.Checked;
            InvokeIntelligenceChanged(new ObjectEventArgs(intelligenceToolStripButton.Checked));
        }

        public event ObjectEventHandler IntelligenceChanged;
        public void InvokeIntelligenceChanged(ObjectEventArgs e)
        {
            ObjectEventHandler handler = IntelligenceChanged;
            if (handler != null) handler(this, e);
        }

        public event EventHandler UpdateNodeEvent;
        public void InvokeUpdateNodeEvent(EventArgs e)
        {
            if (BrowserWindowManager.ActiveBrowser == null) return;
            var handler = UpdateNodeEvent;
            if (handler != null) handler(BrowserWindowManager.ActiveBrowser, e);
        }

        private void UpdateNodeoolStripButtonClick(object sender, EventArgs e)
        {
            InvokeUpdateNodeEvent(e);
        }

        private void TextSearchKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                NavigateNew("http://www.imdb.com/find?s=all&q=" + HttpHelper.UrlEncode(textSearch.Text));
            }
        }

        private void TsCloseAllClick(object sender, EventArgs e)
        {
            BrowserWindowManager.CloseAll();
        }
    }
}
